package com.xysoft.framework.jackson;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.AbstractFactoryBean;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperFactoryBean extends AbstractFactoryBean<ObjectMapper> {
	
	private Module[] modules;

	@Override
	public Class<?> getObjectType() {
		return ObjectMapper.class;
	}

	public void setModules(Module[] modules) {
		this.modules = modules;
	}

	@Override
	protected ObjectMapper createInstance() throws Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		
		if(!ArrayUtils.isEmpty(modules)) {
			for(Module module : modules) {
				objectMapper.registerModule(module);
			}
		}
		
		return objectMapper;
	}

}
